<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="entidades.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>

<html>
<head>

<%@ include file="jspf/head.jspf" %>

<script type="text/javascript" src="js/sp/lineas.js"></script>

</head>
<body>

<%@ include file="jspf/header.jspf" %>

<%
List<Linea> lineas;

try{
	lineas = Linea.listar();
	String accion = request.getParameter("accion");
	accion = (accion != null)? accion : "";

    for(int i=0; i<lineas.size(); i++)
        {
        int id = lineas.get(i).getId();

		if(request.getParameter("linea"+id) != null)
			{
			if(accion.compareTo("guardar") == 0)
				{
				lineas.get(i).setDescripcion(request.getParameter("descripcion_"+id));
				lineas.get(i).setInicial(request.getParameter("inicial_"+id));
				lineas.get(i).setCantidadMinima(request.getParameter("cantidad_minima_"+id).replaceAll("\\.", ""));
				lineas.get(i).guardar();
				}

			if(accion.compareTo("eliminar") == 0)
				{ lineas.get(i).borrar(); }
			}
		}

	if(request.getParameter("linea0") != null && accion.compareTo("guardar") == 0)
		{
		String desc = request.getParameter("descripcion_0");
		String ini = request.getParameter("inicial_0");
		String cant = request.getParameter("cantidad_minima_0").replaceAll("\\.", "");

		if(desc != null && desc.length() > 0 && ini != null && ini.length() > 0 && cant != null && cant.length() > 0)
			{
			Linea l = new Linea();
			l.setDescripcion(desc);
			l.setInicial(ini);
			l.setCantidadMinima(cant);
			l.guardar();
			}
		}
}catch(Exception e) { out.print("<div id=\"div_error_linea\" title=\"Error\">Se produjo un error inesperado:\n" + e.getMessage()+"</div>"); }

lineas = Linea.listar();
%>

<br/>

<h1>ABM Lineas</h1>

<br/>

<!-- Tabla con la lista de resultados -->
<div id="div_resultados">

<form method="post" action="" accept-charset="utf-8" id="form_abm">
	<input type="hidden" name="accion" value=""/>

	<center>

	<table class="resultados">
		<tr class="titulo">
			<td></td>
			<td>Descripción</td>
			<td>Inicial</td>
			<td>Cantidad mínima</td>
		</tr>
	<% { for(int i=0; i<lineas.size(); i++) { %>
		<tr>
			<td><input type="checkbox" name="linea<%= lineas.get(i).getId() %>" value="true"/></td>
			<td><input type="text" class="act_check" name="descripcion_<%= lineas.get(i).getId() %>" size="20" value="<%= lineas.get(i).getDescripcion() %>"/></td>
			<td><input type="text" class="act_check inicial" name="inicial_<%= lineas.get(i).getId() %>" size="1" value="<%= lineas.get(i).getInicial() %>"/></td>
			<td><input type="text" class="act_check numero" name="cantidad_minima_<%= lineas.get(i).getId() %>" size="10" value="<%= Util.numero.format(lineas.get(i).getCantidadMinima()) %>"/></td>
		</tr>
	<% } } %>
		<tr>
			<td><input type="checkbox" name="linea0" value="true"/></td>
			<td><input type="text" class="act_check" name="descripcion_0" size="20" value=""/></td>
			<td><input type="text" class="act_check inicial" name="inicial_0" size="1" value=""/></td>
			<td><input type="text" class="act_check numero" name="cantidad_minima_0" size="10" value=""/></td>
		</tr>
	</table>

	<br/>

	<% if( Sesiones.tienePermiso(session, "lineas", "write")) { %>
	<button name="guardar"><img src="icon/save.png" alt="" title="Guardar"/> <span>Guardar</span></button>
	<button name="eliminar"><img src="icon/delete.png" alt="" title="Eliminar"/> <span>Eliminar</span></button>
	<% } %>

	</center>

</form>

</div>

<%@ include file="jspf/footer.jspf" %>

</body>
</html>
